<?php

class Curl {

    //代理ip
    public $proxy;
    //使用次数
    public $proxy_use_count = 0;
    //是否可用
    public $proxy_can_use = 0;
    //订单id
    public $order_id        = 0;


    var $headers;
    var $compression;
    var $cookie_file;
    var $cookie_jd;


    function __construct($is_cookie = TRUE, $cookie_address = 'cookies.txt', $header = array(), $compression = 'gzip', $proxy = '') {
        if (empty ( $header )) {
            $this->headers [] = 'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
            $this->headers [] = 'Connection: Keep-Alive';
            // $this->headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';
        }
        $this->compression = $compression;
        // $this->proxy=$proxy;
        $this->is_cookie = $is_cookie;
        if ($this->is_cookie == TRUE)
            $this->make_cookie ( $cookie_address );
    }

    function make_cookie($cookie_address) {
        $tmp_filepath = realpath ( './' ) . '/' . $cookie_address;
        if (file_exists ( $tmp_filepath )) {
            $this->cookie_file = $tmp_filepath;
        } else {
            if ($h = fopen ( $tmp_filepath, 'w' )) {
                $this->cookie_file = $tmp_filepath;
                fclose ( $h );
            } else {
                $this->error ( 'The cookie file could not be opened. Make sure this directory has the correct permissions' );
            }
        }
    }

    function set_proxy($proxy) {
        $this->proxy = $proxy;
    }

    function post($url, $data, $refer) {
        $curl = curl_init (); // 启动一个CURL会话
        // $this->headers[] = 'Content-type: application/x-www-form-urlencoded;charset=GBK';
        curl_setopt ( $curl, CURLOPT_HTTPHEADER, $this->headers );
        if (isset ( $this->proxy ))
            curl_setopt ( $curl, CURLOPT_PROXY, $this->proxy );
        curl_setopt ( $curl, CURLOPT_URL, $url ); // 要访问的地址
        curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, 0 ); // 对认证证书来源的检查


        /*
         * curl_setopt(): CURLOPT_SSL_VERIFYHOST no longer accepts the value 1, value 2 will be used instead in C:\phproot\rush\trunk\Cls\curl.php on line 50
         * */
        //
        @curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, 1 ); // 从证书中检查SSL加密算法是否存在
        curl_setopt ( $curl, CURLOPT_USERAGENT, $this->user_agent () ); // 模拟用户使用的浏览器
        // curl_setopt($curl, CURLOPT_NOBODY, 0); //不获取内容
        if ($this->is_cookie == TRUE)
            curl_setopt ( $curl, CURLOPT_COOKIEFILE, $this->cookie_file );
        if ($this->is_cookie == TRUE)
            curl_setopt ( $curl, CURLOPT_COOKIEJAR, $this->cookie_file );
        curl_setopt ( $curl, CURLOPT_FOLLOWLOCATION, 1 ); // 使用自动跳转
        if (! isset ( $refer )) {
            curl_setopt ( $curl, CURLOPT_AUTOREFERER, 1 ); // 自动设置Referer
        } else {
            curl_setopt ( $curl, CURLOPT_REFERER, $refer );
        }
        curl_setopt ( $curl, CURLOPT_POST, 1 ); // 发送一个常规的Post请求
        curl_setopt ( $curl, CURLOPT_POSTFIELDS, $data ); // Post提交的数据包
        curl_setopt ( $curl, CURLOPT_TIMEOUT, 30 ); // 设置超时限制防止死循环
        curl_setopt ( $curl, CURLOPT_HEADER, 0 ); // 显示返回的Header区域内容
        curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 ); // 获取的信息以文件流的形式返回
        $result ['content'] = curl_exec ( $curl ); // 执行操作 $curl_info=
        // preg_match_all('/Set-Cookie:(.*)Path\=\//iU',$curl_info,$str); //正则匹配
        // print_r($str[0]); //获得COOKIE（SESSIONID）
        $curl_info = curl_getinfo ( $curl ); // 获取请求头信息
        if ($curl_info ['http_code'] != '200') 		// 判断状态码 决定是否重试
        {
            sleep ( 5 ); // 稍后重试
            curl_exec ( $curl ); // 执行操作
            $curl_info = curl_getinfo ( $curl );
        }

        $result ['curl_info'] = $curl_info;
        $result ['dateline'] = time ();
        if (curl_errno ( $curl )) {
            // return 'Error'.curl_error($curl); //抓取异常
            return FALSE;
        }
        curl_close ( $curl ); // 关闭CURL会话
        return $result; // 返回数据
    }

    function user_agent() {
        $ua = array (
            '0' => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)', // ie7
            '1' => 'Mozilla/5.0 (Windows NT 6.1; Intel Mac OS X 10.6; rv:7.0.1) Gecko/20100101 Firefox/7.0.1',
            '2' => 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)',
            '3' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)',
            '4' => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Maxthon 2.0)',
            '5' => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; The World)',
            '6' => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; TencentTraveler 4.0; .NET CLR 2.0.50727)',
            '7' => 'Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1',
            '8' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11'
        );
        $suiji = rand ( 0, 8 );
        return $ua [$suiji];
    }

    function get($url, $refer=null) { // 模拟提交数据函数
        $curl = curl_init (); // 启动一个CURL会话
        // $this->headers[] = 'Content-type: application/x-www-form-urlencoded;charset=GBK';
        curl_setopt ( $curl, CURLOPT_HTTPHEADER, $this->headers );
        curl_setopt ( $curl, CURLOPT_URL, $url ); // 要访问的地址
        curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, 0 ); // 对认证证书来源的检查
        //运行有错，
        /*otice: curl_setopt(): CURLOPT_SSL_VERIFYHOST no longer accepts the value 1, value 2 will be used instead in C:\phproot\rush\trunk\Cls\curl.php on line 111*/
        @curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, 1 ); // 从证书中检查SSL加密算法是否存在
        curl_setopt ( $curl, CURLOPT_USERAGENT, $this->user_agent() ); // 模拟用户使用的浏览器
        // curl_setopt($curl, CURLOPT_NOBODY, 0);
        // echo $this->proxy;
        if (isset ( $this->proxy )){
            curl_setopt ( $curl, CURLOPT_PROXY, $this->proxy );
        }
        // curl_setopt ($curl, CURLOPT_PROXYUSERPWD,'yushi:cxjb9ade');

        // curl_setopt ($curl, CURLOPT_PROXYTYPE,"CURLPROXY_SOCKS5");
        curl_setopt ( $curl, CURLOPT_FOLLOWLOCATION, 1 ); // 使用自动跳转
        if (! isset ( $refer )) {
            curl_setopt ( $curl, CURLOPT_AUTOREFERER, 1 ); // 自动设置Referer
        } else {
            curl_setopt ( $curl, CURLOPT_REFERER, $refer );
        }
        // curl_setopt($curl, CURLOPT_POST, 0); // 发送一个常规的Post请求
        // curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
        curl_setopt ( $curl, CURLOPT_TIMEOUT, 120 ); // 设置超时限制防止死循环
        curl_setopt ( $curl, CURLOPT_HEADER, 0 ); // 显示返回的Header区域内容
        curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 ); // 获取的信息以文件流的形式返回
        if ($this->is_cookie == TRUE)
            curl_setopt ( $curl, CURLOPT_COOKIEFILE, $this->cookie_file );
        if ($this->is_cookie == TRUE)
            curl_setopt ( $curl, CURLOPT_COOKIEJAR, $this->cookie_file );
        $result ['content'] = curl_exec ( $curl ); // 执行操作
        $curl_info = curl_getinfo ( $curl );
        if ($curl_info ['http_code'] != '200') 		// 判断状态码 决定是否重试
        {
            sleep ( 5 ); // 稍后重试
            curl_exec ( $curl ); // 执行操作
            $curl_info = curl_getinfo ( $curl );
        }
        $result ['header'] = $curl_info;
        $result ['error'] = curl_errno ( $curl );
        if ($result ['error']) {
            return FALSE;
        }
        // preg_match_all('/^Set-Cookie: (.*?);/m', $curl_info, $tmp_filepath_cookie_jd);
        // $this->cookie_jd=$tmp_filepath_cookie_jd[1];
        // print_r($this->cookie_jd);
        curl_close ( $curl ); // 关闭CURL会话
        return $result; // 返回数据
    }
}
?>